#include "projectview.h"
#include "projectitem.h"
#include <QAction>

ProjectView::ProjectView(QWidget *parent) :
  QTreeView(parent) {
  setAnimated(false);
  setIndentation(10);
  setSortingEnabled(true);
  setEditTriggers(QAbstractItemView::NoEditTriggers);
  setContextMenuPolicy(Qt::ActionsContextMenu);
  setMinimumWidth(150);
  setMaximumWidth(150);
  addItem = new QAction("Add New...", this);
  remove = new QAction("Remove...", this);
  rename = new QAction("Rename", this);
  addAction(addItem);
  addAction(remove);
  addAction(rename);
  connect(addItem, SIGNAL(triggered()), this, SIGNAL(addItemRequested()));
  connect(remove, SIGNAL(triggered()), this, SIGNAL(removeRequested()));
  connect(rename, SIGNAL(triggered()), this, SIGNAL(renameRequested()));
}

void ProjectView::selectionChanged(const QItemSelection &selected, const QItemSelection &deselected) {
  if (selected.count() < 1) {
    return;
  }

  addItem->setEnabled(true);
  QModelIndex index = currentIndex();

  if (!index.isValid()) {
    return;
  }

  ProjectItem *item = static_cast<ProjectItem*>(index.internalPointer());
  QString nodeName = item->node().nodeName();

  if (nodeName != "Codes" && nodeName != "Statics" && nodeName != "Project"
          && nodeName != "Src") {
    addItem->setEnabled(false);
  }
}
